package org.example.common.bean;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用业务分页实体类.
 *
 * @author dingcan
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonPage<T> implements Serializable {
    
    private static final long serialVersionUID = -3050319652690913471L;
    
    /**
     * 结果数据
     */
    private List<T> data = new ArrayList<>(1);
    
    /**
     * 数据总量
     */
    private long count = 0;
    
    /**
     * 页码
     */
    private int pageNum = 1;
    
    /**
     * 每页数据量
     */
    private int pageSize = 10;
    
    
    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        CommonPage<T> result = new CommonPage<>();
        PageInfo<T> pageInfo = new PageInfo<>(list);
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    
    public static <T> CommonPage<T> restPage(PageInfo<T> pageInfo) {
        CommonPage<T> result = new CommonPage<>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    
    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
        CommonPage<T> result = new CommonPage<>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setCount(pageInfo.getTotal());
        result.setData(pageInfo.getResult());
        return result;
    }
    
    public static <T> CommonPage<T> restPage(List<T> data, long total, int pageNum, int pageSize) {
        CommonPage<T> result = new CommonPage<>();
        result.setPageNum(pageNum);
        result.setPageSize(pageSize);
        result.setCount(total);
        result.setData(data);
        return result;
    }
    
    public static <T> CommonPage<T> restPage(List<T> data, long total, int pageNum) {
        CommonPage<T> result = new CommonPage<>();
        result.setPageNum(pageNum);
        result.setPageSize(10);
        result.setCount(total);
        result.setData(data);
        return result;
    }
    
    public static <T> CommonPage<T> restPage(List<T> data, long total) {
        CommonPage<T> result = new CommonPage<>();
        result.setCount(total);
        result.setData(data);
        return result;
    }
}
